package com.nanfang51g3.ckz.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.nanfang51g3.ckz.utils.KLog;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016/7/5.
 */
public class ImmortalService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        startDaemon(this, this.getPackageName());

    }

    // 瀹堟姢杩涚▼鏄惁鍦ㄨ繍琛屼腑锛屼笉鏄痑ndroid app杩涚▼锛屼笉鑳介�氳繃ActivityManager鑾峰彇鍒拌繘绋嬶紝閫氳繃ps鍛戒护鑾峰彇
    private boolean isdaemonLive() {
        String processName = "/data/data/" + getPackageName() + "/chkproc";
        Process process = null;
        DataInputStream inputStream = null;
        DataOutputStream outputStream = null;
        DataInputStream erro = null;
        try {
            process = Runtime.getRuntime().exec("sh"); // 鑾峰緱shell.
            inputStream = new DataInputStream(process.getInputStream());
            outputStream = new DataOutputStream(process.getOutputStream());
            erro = new DataInputStream(process.getInputStream());

            outputStream.writeBytes("ps | grep " + getPackageName() + " \n");
            outputStream.writeBytes("exit\n");
            outputStream.flush();
            process.waitFor();
            String line = null;
            while ((line = inputStream.readLine()) != null) {
                KLog.i("Evd", "output : " + line);
                if (line != null && line.contains(processName))
                    return true;
            }

        } catch (Exception e) {
            return false;
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return false;

    }

    public void startDaemon(Context context, String packageName) {
        if (isdaemonLive()) {
            return;
        }
        // String result = CmdExcUtil.execCmd("ps");
        // if(!result.contains("chkproc")){
        KLog.i("Evd", "*********chkproc*************");
        String path = "/data/data/" + packageName;
        String cmd1 = path + "/libs/libchkproc.so";
        String cmd2 = path + "/chkproc";
        String cmd3 = "chmod 777 " + cmd2;
        String cmd4 = "dd if=" + cmd1 + " of=" + cmd2;
        String cmd5 = path + "/chkproc -p " + packageName + " -a "
                + packageName
                + " -c com.nanfang51g3.ckz.ui.welcome.WelcomeActivity -i 10";
        // if(!new File(cmd2).exists()){
        execCommand(cmd4, packageName); // 鎷疯礉lib/libtest.so鍒颁笂涓�灞傜洰褰�,鍚屾椂鍛藉悕涓簍est.
        execCommand(cmd3, packageName); // 鏀瑰彉test鐨勫睘鎬�,璁╁叾鍙樹负鍙墽琛�
        // }
        execCommand(cmd5, packageName); // 鎵цtest绋嬪簭.
        // }
    }

    public static boolean execCommand(String command, String packageName) {
        Process process = null;
        DataInputStream inputStream = null;
        DataOutputStream outputStream = null;
        DataInputStream erro = null;
        try {
            process = Runtime.getRuntime().exec("sh"); // 鑾峰緱shell.
            inputStream = new DataInputStream(process.getInputStream());
            outputStream = new DataOutputStream(process.getOutputStream());
            erro = new DataInputStream(process.getInputStream());

            outputStream.writeBytes("cd /data/data/" + packageName + "\n"); // 淇濊瘉鍦╟ommand鍦ㄨ嚜宸辩殑鏁版嵁鐩綍閲屾墽琛�,鎵嶆湁鏉冮檺鍐欐枃浠跺埌褰撳墠鐩綍

            outputStream.writeBytes(command + " \n");
            outputStream.writeBytes("exit\n");
            outputStream.flush();
            process.waitFor();
            String line = null;
            KLog.i("Evd", command);
            while ((line = inputStream.readLine()) != null) {
                KLog.i("Evd", "output : " + line);
            }

            while ((line = erro.readLine()) != null) {
                KLog.i("Evd", "error : " + line);
            }

        } catch (Exception e) {
            return false;
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        KLog.i("Evd", "service onDestroy");
    }

}
